package os.kai.aiu;

import nl.siegmann.epublib.domain.Book;
import nl.siegmann.epublib.domain.Resource;
import nl.siegmann.epublib.domain.SpineReference;
import nl.siegmann.epublib.epub.EpubReader;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

public class ConvertEpubNovel {
    private static void convert(String input, String outputPrefix, String outputSuffix, String outputEncoding) throws IOException {
        try(IncreaseWriter writer = new IncreaseWriter(outputPrefix,outputSuffix,outputEncoding,0)){
            EpubReader reader = new EpubReader();
            try(InputStream ins = Files.newInputStream(Paths.get(input))){
                Book book = reader.readEpub(ins);
                List<SpineReference> refs = book.getSpine().getSpineReferences();
                int size = refs.size();
                for(int i=0; i<size; i++){
                    Resource resource = refs.get(i).getResource();
                    writer.setIndex(i+1);
                    try(BufferedReader resReader = new BufferedReader(resource.getReader())){
                        String line;
                        while((line = resReader.readLine())!=null){
                            writer.println(line);
                        }
                    }
                }
            }
        }
    }

    public static void convertWuZhi() throws IOException {
        String input = "D:\\work\\aigc\\novels\\无职转生\\無職轉生(web版譯名統一版)(全卷完).epub";
        String outputPrefix = "D:\\work\\aigc\\novels\\无职转生\\split\\wuzhi_";
        String outputSuffix = ".html";
        convert(input,outputPrefix,outputSuffix,"UTF-8");
    }

    public static void convertLuRen() throws IOException {
        String input = "D:\\work\\aigc\\novels\\乙女路人\\乙女游戏世界对路人角色太严厉 1-SS17.epub";
        String outputPrefix = "D:\\work\\aigc\\novels\\乙女路人\\split\\luren_";
        String outputSuffix = ".html";
        convert(input,outputPrefix,outputSuffix,"UTF-8");
    }

    public static void main(String[] args) throws IOException {
        convertLuRen();
    }
}
